import Vue from 'vue'
import App from './App.vue'
import router from './router'

// 引入axios
import axios from './axios'
Vue.prototype.$axios = axios;

// 引入utils
import zyc from './utils/zyc';
Vue.prototype.$zyc = zyc;

Vue.config.productionTip = false

// 引入Element-ui
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

// 自动注册全局组件
import lodash from "lodash";
const components = require.context("./components", true, /\.vue$/);
components.keys().forEach(fileName => {
    const cmp = components(fileName);

    var pos = fileName.lastIndexOf("/");
    var name = fileName.substr(pos + 1);
    var pos1 = name.lastIndexOf(".");
    if (pos1 > 0) {
        name = name.substr(0, pos1);
    }
    name = lodash.upperFirst(lodash.camelCase(name));

    Vue.component(name, cmp.default || cmp);
});

// 动态调用组件
Vue.prototype.$install = (name, options) => {
    if (!options) {
        options = {};
    }

    // var component = Vue.extend(Vue.component(name));
    var component = Vue.component(name);
    if (!component) {
        console.error("component", name, "not exists!");
        return;
    }
    var cmp = new component({
        router: router,
        store: store,
        propsData: options.props ?
            Object.assign({
                    visible: true
                },
                options.props
            ) : {
                visible: true
            }
    });
    if (options.events) {
        for (var p in options.events) {
            cmp.$on(p, options.events[p]);
        }
    }
    if (options.el) {
        cmp.$mount(options.el);
    } else {
        var render = document.createElement("div");
        cmp.$mount(render);
        document.body.appendChild(render);
    }
};

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')